package com.fivestar.pmanager.web.business.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fivestar.pmanager.core.base.BaseCommonParameterBean;
import com.fivestar.pmanager.core.exception.DMLNoRecordEffectedException;
import com.fivestar.pmanager.core.util.CodeHelper;
import com.fivestar.pmanager.model.FsUserBean;
import com.fivestar.pmanager.model.SysRoleUserBean;
import com.fivestar.pmanager.service.FsUserService;
import com.fivestar.pmanager.service.SysRoleUserService;

/**
 * 用户角色业务服务
 *
 * Created by admin
 */
@Service("sysRoleUserBusiService")
public class SysRoleUserBusiService
{

    @Autowired
    private SysRoleUserService sysRoleUserService;
    @Autowired
    private FsUserService fsUserService;

    /**
     * 保存用户角色
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void addUserRole(int userId, String roleIds, BaseCommonParameterBean commonParam)throws DMLNoRecordEffectedException{
    	//先删除 再新增
    	SysRoleUserBean temp = new SysRoleUserBean();
    	temp.setUserId(userId);
    	sysRoleUserService.deleteBySelective(temp, commonParam);
    	
    	String[] roleIdArr = roleIds.split(",");
    	for (String roleId : roleIdArr) {
    		SysRoleUserBean smRoleUser = new SysRoleUserBean();
    		smRoleUser.setUserId(userId);
    		smRoleUser.setRoleId(Integer.valueOf(roleId));
    		sysRoleUserService.insert(smRoleUser, commonParam);
		}
    }
    
    /**
     * 保存用户角色
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void addUserRole2(int roleId, String userIds, BaseCommonParameterBean commonParam)throws DMLNoRecordEffectedException{
    	//先删除 再新增
    	SysRoleUserBean temp = new SysRoleUserBean();
    	temp.setRoleId(roleId);
    	sysRoleUserService.deleteBySelective(temp, commonParam);
    	
    	String[] userIdArr = userIds.split(",");
    	for (String userId : userIdArr) {
    		SysRoleUserBean smRoleUser = new SysRoleUserBean();
    		smRoleUser.setUserId(Integer.parseInt(userId));
    		smRoleUser.setRoleId(roleId);
    		sysRoleUserService.insert(smRoleUser, commonParam);
		}
    }
    
    /**
     * 删除用户角色  by id
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteUser(int id, BaseCommonParameterBean commonParam)throws DMLNoRecordEffectedException
    {
    	delUserProcess(id,commonParam);
    }
    
    /**
     * 删除用户角色  by id string
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatchUser(String ids, BaseCommonParameterBean commonParam)throws DMLNoRecordEffectedException
    {
    	String[] rowIdStr = ids.split(",");
    	for (int i = 0; i < rowIdStr.length; i++) 
    	{
    		if(CodeHelper.isNull(rowIdStr[i]))continue;
    		delUserProcess(Integer.valueOf(rowIdStr[i]),commonParam);
		}
    }
    
    private void delUserProcess(int id,BaseCommonParameterBean commonParam) throws DMLNoRecordEffectedException
    {
    	FsUserBean delUserBean = new FsUserBean();
    	delUserBean.setRowId(id);
    	fsUserService.delete(delUserBean, commonParam);
    }



}
